package com.id.sigma;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.json.simple.JSONObject;

import com.google.gson.*;

public class ManageBrokeSingleton {

	private static ManageBrokeSingleton instance;
	private static SessionFactory factory;
	private Gson gson;
	private JSONObject obj;

	public static ManageBrokeSingleton getInstance() {
		if (instance == null)
			instance = new ManageBrokeSingleton();
		return instance;
	}

	public ManageBrokeSingleton() {
		factory = new Configuration().configure().buildSessionFactory();
		gson = new Gson();
	}

	public String addBroker(Integer id, String name, String alamat, long saldoUang,
			long saldoSaham) {
		obj = new JSONObject();
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Broker broker = new Broker(id, name, alamat, saldoUang, saldoSaham);
			session.save(broker);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			obj.put("status", "Gagal insert");
			return obj.toJSONString();
		} finally {
			session.close();
		}
		obj.put("status", "sukses insert");
		return obj.toJSONString();
	}

	/* Method to READ all the employees */
	public String listBrokers() {
		Session session = factory.openSession();
		Transaction tx = null;
		List<Broker> brokers = null;
		try {
			tx = session.beginTransaction();
			brokers = session.createQuery("FROM Broker").list();
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return gson.toJson(brokers);

	}

	public String getBroker(Integer id) {

		Session session = factory.openSession();
		Transaction tx = null;
		Broker broker = null;
		try {
			tx = session.beginTransaction();
			broker = (Broker) session.createQuery(
					"FROM Broker B where B.idBroker=" + id).uniqueResult();
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null) {
				tx.rollback();
				return "not found";
			}
			e.printStackTrace();
		} finally {
			session.close();
		}
		return gson.toJson(broker);
	}

	/* Method to UPDATE salary for an employee */
	public String updateBrokers(Integer id, String name, String alamat,
			long saldoUang, long saldoSaham) {
		obj = new JSONObject();
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Broker broker = (Broker) session.get(Broker.class, id);
			broker.setAlamat(alamat);
			broker.setNamaBroker(name);
			broker.setSaldoSaham(saldoSaham);
			broker.setSaldoUang(saldoUang);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			obj.put("status", "gagal update");
			return obj.toJSONString();
		} finally {
			session.close();
		}
		obj.put("status", "sukses update");
		return obj.toJSONString();
	}

	/* Method to DELETE an employee from the records */
	public String deleteBroker(Integer id) {
		obj = new JSONObject();
		Session session = factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Broker broker = (Broker) session.get(Broker.class, id);
			session.delete(broker);
			tx.commit();
			obj.put("status", "gagal delete");
			return obj.toJSONString();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		obj.put("status", "sukses delete");
		return obj.toJSONString();
	}
}
